package com.hyy.blogtest.common.utils;

import com.hyy.blogtest.common.pojo.dadaobject.BlogInfo;
import com.hyy.blogtest.common.pojo.dadaobject.UserInfo;
import com.hyy.blogtest.common.pojo.request.AddBlogRequest;
import com.hyy.blogtest.common.pojo.request.RegisterRequest;
import com.hyy.blogtest.common.pojo.request.UpdateBlogRequest;
import com.hyy.blogtest.common.pojo.response.BlogInfoResponse;
import com.hyy.blogtest.common.pojo.response.UserInfoResponse;
import org.springframework.beans.BeanUtils;

public class BeanUtilsConver {
    public static UserInfoResponse converToUser(UserInfo userInfo) {
        if(userInfo==null)return null;
        UserInfoResponse userInfoResponse = new UserInfoResponse();
        BeanUtils.copyProperties(userInfo, userInfoResponse);
        return userInfoResponse;
    }

    public static BlogInfoResponse converToBlog(BlogInfo blogInfo) {
        if(blogInfo==null)return null;
        BlogInfoResponse blogInfoResponse = new BlogInfoResponse();
        BeanUtils.copyProperties(blogInfo, blogInfoResponse);

        //截取一部分字符作为总概括
        if(blogInfo.getContent().length()<=30){
            blogInfoResponse.setOverview(blogInfo.getContent());
        }else{
            blogInfoResponse.setOverview(blogInfo.getContent().substring(0,30)+"...");
        }
        return blogInfoResponse;
    }


    public static BlogInfo conver(AddBlogRequest addBlogRequest) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(addBlogRequest, blogInfo);
        return blogInfo;
    }

    public static BlogInfo conver(UpdateBlogRequest updateBlogRequest) {
            BlogInfo blogInfo = new BlogInfo();
            BeanUtils.copyProperties(updateBlogRequest, blogInfo);
            return blogInfo;
    }

    public static UserInfo conver(RegisterRequest request) {
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(request, userInfo);
        return userInfo;
    }
}
